<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\common\model\Post\Cate as CateModel;
use app\common\validate\Post\Cate as CateValidate;
use think\exception\ValidateException;

class Cate extends BaseController
{
    protected $middleware = [
        \app\admin\middleware\LoginAuth::class
    ];

    // 帖子分类
    public function index(){
        $menu = [
            'name' => '问答管理',
            'title' => '分类列表'
        ];
        $searchfield = $this->request->param('search_field', 'name');
        $keyword = $this->request->param('keyword');
        $where = [];
        if ($searchfield && $keyword) {
            $where[] = [$searchfield, 'like', "%$keyword%"];
        }
        // 每页显示数量
        $num = 8;
        $CateList = CateModel::where($where)->order('weigh', 'desc')->paginate(['list_rows' => $num, 'query' => request()->param()]);
        // 当前页
        $page = $CateList->currentPage();
        return view('', [
            'menu' => $menu,
            'CateList' => $CateList,
            'num' => $num,
            'page' => $page,
            'searchfield' => $searchfield,
            'keyword' => $keyword
        ]);
    }
    // 新增分类
    public function add(){
        $menu = [
            'name' => '问答管理',
            'title' => '新增分类'
        ];
        if($this->request->isPost()){
            $name = $this->request->param('name');
            $weigh=$this->request->param('weigh');
            $data=[
                'name' => $name,
                'weigh' => $weigh
            ];
            try {
                Validate(CateValidate::class)->check($data);
            } catch (ValidateException $e) {
                $this->error($e->getMessage());
            }
            $result = CateModel::create($data);
            if ($result === false) {
                $this->error('新增失败');
            }else{
                $this->success('新增成功',(string)url('admin/cate/index'));
            }
        }
        return view('', [
            'menu' => $menu,
        ]);
    }
    //编辑分类
    public function edit(){
        $menu = [
            'name' => '问答管理',
            'title' => '分类编辑'
        ];

        $id= $this->request->param('cateid');
        $Cate=CateModel::find($id);

        if(!$Cate){
            $this->error('分类不存在！');
        }
        if($this->request->isPost()){
            $name=$this->request->post('name');
            $weigh=$this->request->post('weigh');

            $data=[];
            if(!empty($name)){
                $data['name']=$name;
            }
            if(!empty($weigh)){
                $data['weigh']=$weigh;
            }
            $result=CateModel::where(['id' => $id])->save($data);
            if($result !== FALSE){
                $this->success('修改分类成功',(string)url('admin/cate/index'));
            }else{
                $this->error('修改分类失败');
            }
        }
        return view('', [
            'menu' => $menu,
            'Cate' => $Cate,
        ]);
    }
    // 删除分类
    public function del(){
        if($this->request->isAjax()){
            $action = $this->request->param('action');
            $ids = $this->request->param('ids');
            $cateid = $this->request->param('cateid');
            //单条删除
            if ($action == 'Del') {
                $Cate = CateModel::find($cateid);
                if (!$Cate) {
                    $this->error('分类不存在');
                }
                $result = CateModel::where(['id'=>$cateid])->delete();
                if ($result === false) {
                    $this->error('删除失败');
                } else {
                    $this->success('删除成功');
                }
            }
            if ($action == 'BatchDelete') {
                $CateList = CateModel::whereIn('id', $ids)->select();
                foreach ($CateList as $item) {
                    if (!in_array($item['id'], $ids)) {
                        $this->error("所选分类{$item['name']}不存在,请重新选择");
                    }
                }
                $result = CateModel::where(['id' => $ids])->delete();
                if ($result !== FALSE) {
                    $this->success('批量删除成功');
                } else {
                    $this->error("批量删除失败");
                }
            }
        }
    }
}
